//int __stdcall AlphaBlit_8u_C4A(unsigned char* pSrc, int srcStep, unsigned char* pAlp, int AlpStep, unsigned char* pDst, int dstStep, IppiSize roiSize, int scaleFactor);


#include <asm.h>

.global C_SYMBOL(Add_8u_C4A)

C_SYMBOL(Add_8u_C4A):


    STMFD	SP!, {R4-R12,LR}
Start:
    MOV	LR, R0

    LDR R11, [LR, #0x0]                 //pSrc
    LDR	R8,  [LR, #0x8]                 //SrcStep
    LDR	R10, [LR, #0x4]                //pDst
	LDR	R12, [LR, #0xC]                //DstStep

    LDR	R4, [LR, #0x10]                 //width
	LDR	R7, [LR, #0x14]                 //height

	MOV LR, R4, LSL #2

    LDR     R4, = 0x00FF00FF
    TMCRR   wR15, R4, R4

    LDR     R4, = 0x00000008
    TMCR    wCGR3, R4

LoopLine:
	MOV	R3, LR
	MOV	R2, R10
	MOV	R0, R11
	B Alpha_8u_C4ARow
Next:
	SUB	R7, R7,	#1
	ADD	R11, R11, R8
	ADD	R10, R10, R12
	CMP	R7, #0
	BHI	LoopLine

End:

	LDMFD	SP!, {R4-R12,PC}


Alpha_8u_C4ARow:

    ADD	R6, R2,	#7
    BIC	R6, R6,	#7
    SUB	R6, R6,	R2
    CMP	R6, #0
    BEQ	_Aligned8_

_Process_One_:

    WLDRW   wR4, [R0], #4
    CMP	R6, R3
    WLDRW   wR6, [R2]

    MOVGT	R6, R3
    SUB	R3, R3,	R6

_Loop_One_:
//.long 0xe7fddefe
    SUBS	R6, R6,	#4

    WLDRWGT   wR4, [R0], #4
    WADDBUS   wR6, wR4, wR6
    WLDRWGT   wR6, [R2, #4]

    WSTRW   wR6, [R2], #4

    BGT	_Loop_One_

    CMP	R3, #0
    BEQ     Next

_Aligned8_:
    SUBS	R3, R3,	#0x10
    ADDMI	R6, R3,	#0x10
    ADDMI	R3, R3,	#0x10
    BMI	_Process_One_
    AND	R4, R0,	#7
    CMP R4, #0
    BNE	_UnAligned8_

    WLDRD   wR0, [R0], #8
    WLDRD   wR4, [R2]

    WLDRD   wR2, [R0], #8
    WLDRD   wR5, [R2, #8]

_Loop_Aligned8_:

    SUBS	R3, R3,	#0x10
    PLD	[R0,#0x20]
    PLD	[R2,#0x30]

    WADDBUS wR4, wR0, wR4
    WLDRDGE wR0, [R0], #8
    WSTRD   wR4, [R2], #8
    WLDRDGE wR4, [R2, #8]
    WADDBUS wR5, wR2, wR5
    WLDRDGE wR2, [R0], #8
    WSTRD   wR5, [R2], #8
    WLDRDGE wR5, [R2, #8]

    BGE	_Loop_Aligned8_
    ADDS	R3, R3,	#0x10
    BEQ     Next
    MOVGT	R6, R3
    //SUBGT   R2, R2, #8
    BGT	_Process_One_

_UnAligned8_:
    BIC	R0, R0,	#7
    TMCR    wCGR1, R4

    WLDRD   wR0, [R0], #8
    WLDRD   wR4, [R2]

    WLDRD   wR2, [R0], #8
    WLDRD   wR5, [R2, #8]

_Loop_UnAligned8_:
@.long 0xe7fddefe
    TST	R4, #7
    WLDRDNE wR12, [R0]

    SUBS	R3, R3,	#0x10
    PLD	[R0,#0x20]
    PLD	[R2,#0x30]

    WALIGNR1 wR0 ,wR0, wR2
    WADDBUS wR4, wR0, wR4

    WLDRDGE wR0, [R0], #8
    WSTRD   wR4, [R2], #8
    WLDRDGE wR4, [R2, #8]

    WALIGNR1 wR2 ,wR2, wR12
    WADDBUS  wR5 ,wR5, wR2

    WLDRDGE wR2, [R0], #8
    WSTRD   wR5, [R2], #8
    WLDRDGE wR5, [R2, #8]

    BGE	_Loop_UnAligned8_

    ADD	R0, R0,	R4
    ADDS	R3, R3,	#0x10
    BEQ     Next
    MOVGT	R6, R3
    BGT	_Process_One_
